/****************************************************************************
 * Copyright 2009 Huascar A. Sanchez                                        *
 *                                                                          *
 * Licensed under the Apache License, Version 2.0 (the "License");          *
 * you may not use this file except in compliance with the License.         *
 * You may obtain a copy of the License at                                  *
 *                                                                          *
 *     http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                          *
 * Unless required by applicable law or agreed to in writing, software      *
 * distributed under the License is distributed on an "AS IS" BASIS,        *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
 * See the License for the specific language governing permissions and      *
 * limitations under the License.                                           *
 ****************************************************************************/
package com.gotobject.junit.runners;

import static com.gotobject.filter.XmlFilterQ.from;
import com.gotobject.filter.spi.XmlSource;
import com.gotobject.junit.ConfigureWith;
import com.gotobject.junit.internal.runners.model.Explosion;
import org.junit.runner.Runner;
import org.junit.runners.Suite;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
import org.w3c.dom.Element;

import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

/**
 * @author huascar.sanchez@gmail.com (Huascar A. Sanchez)
 */
public class JUnitPerfSuite extends Suite {
    public JUnitPerfSuite(Class<?> root) throws InitializationError {
        super(new JUnitPerfRunnerBuilder(root), root, new Class[]{});
    }

    /**
     * help class that allows me to build a list of explosions.
     */
    private static class ExplosionBuilder {
        private final List<Explosion> explosions;
        ExplosionBuilder(){
            explosions = new ArrayList<Explosion>();
        }

        ExplosionBuilder addLoadTests(Iterable<Element> load) {
            for(Element eachLoad : load){
                final String users = eachLoad.getAttribute("users");
                final String iterations = eachLoad.getAttribute("iterations");
                final Explosion loadExplosion = new Explosion(Integer.valueOf(users), Integer.valueOf(iterations));
                loadExplosion.forTest("load");
                explosions.add(loadExplosion);
            }
            return this;
        }

        ExplosionBuilder addResponseTests(Iterable<Element> response) {
            for(Element eachResponse : response){
                final String users = eachResponse.getAttribute("users");
                final String iterations = eachResponse.getAttribute("iterations");
                final String timeOut = eachResponse.getAttribute("timeout");
                final Explosion loadExplosion = new Explosion(Integer.valueOf(users), Integer.valueOf(iterations), Long.valueOf(timeOut));
                loadExplosion.forTest("response");
                explosions.add(loadExplosion);
            }
            return this;
        }

        ExplosionBuilder addThroughputTest(Iterable<Element> throughput) {
            for(Element eachResponse : throughput){
                final String users = eachResponse.getAttribute("users");
                final String iterations = eachResponse.getAttribute("iterations");
                final String timeOut = eachResponse.getAttribute("timeout");
                final Explosion loadExplosion = new Explosion(Integer.valueOf(users), Integer.valueOf(iterations), Long.valueOf(timeOut));
                loadExplosion.forTest("throughput");
                explosions.add(loadExplosion);
            }
            return this;
        }

        Iterable<Explosion> build() {
            return Collections.unmodifiableList(explosions);
        }
    }

    /**
     *  Represents a Performance Runner Builder.
     */
    private static class JUnitPerfRunnerBuilder extends RunnerBuilder {
        final ConfigureWith  config;
        final String         fileName;
        final List<Class<?>> classes;

        JUnitPerfRunnerBuilder(Class<?> root){
            config   = root.getAnnotation(ConfigureWith.class);
            fileName = config.value();
            classes  = new ArrayList<Class<?>>();
        }

        @Override
        public Runner runnerForClass(Class<?> testClass) throws Throwable {
            final File  file    = new File(fileName);
            final URI   uri     = file.toURI();
            final Iterable<Element> load        = from(XmlSource.load(uri.toURL())).elements("load").select();
            final Iterable<Element> response    = from(XmlSource.load(uri.toURL())).elements("response").select();
            final Iterable<Element> throughput  = from(XmlSource.load(uri.toURL())).elements("throughput").select();

            return new ConditionalJUnitPerfClassRunner(
                    testClass,
                    new ExplosionBuilder()
                    .addLoadTests(load)
                    .addResponseTests(response)
                    .addThroughputTest(throughput)
            .build());
        }

    }
}
